package context.control;

import context.com.baidu.ueditor.define.FileType;
import context.dao.*;
import context.model.Comment;
import context.model.Dynamic;
import context.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.MultipartFile;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.SimpleFormatter;

@RestController
@RequestMapping(value = "/")
public class RestfulControl {

    @Autowired
    HttpSession session;

    @Autowired
    DynamicRepository mDynamicRepo;

    @Autowired
    UserRepository mUserRepo;

    @Autowired
    CommentRepository commentRepository;

    @Autowired
    Thumbs_upRepository thumbs_upRepository;

    @RequestMapping(value = "/uploadimage")
    public Map<String,Object> uploadImage(@RequestParam MultipartFile upfile, HttpServletRequest request) throws IOException {
        String basePath = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/");
        System.out.println("当前应用上下文目录："+basePath);

        String filename=upfile.getOriginalFilename();
        System.out.println("原始文件名："+filename);
        String suffix = FileType.getSuffixByFilename(filename);
        Map<String,Object> state=new HashMap<>();

        String staticResDir=basePath+"staticresource";
        String staticImgResDir=staticResDir+File.separator+"image";

        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");

        Date date=new Date(System.currentTimeMillis());
        String d=dateFormat.format(date);
        System.out.println("日期："+d);

        staticImgResDir+=File.separator+d;
        System.out.println(staticImgResDir);

        File file=new File(staticImgResDir);
        if(!file.exists()){
            file.mkdirs();
        }

        upfile.transferTo(new File(staticImgResDir,filename));
        state.put("state","SUCCESS");

        String url="staticresource"+"/"+"image"+"/"+d+"/"+filename;
        System.out.println("url:"+url);

        state.put("url",url);

        state.put("type",suffix);
        state.put("title",filename);
        state.put("original",filename);
        return state;
    }

    @RequestMapping(value = "/uploadvideo")
    public Map<String,Object> uploadVideo(@RequestParam MultipartFile upfile,HttpServletRequest request) throws IOException {
        String basePath = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/");
        System.out.println("当前应用上下文目录："+basePath);

        String filename=upfile.getOriginalFilename();
        System.out.println("原始文件名："+filename);
        String suffix = FileType.getSuffixByFilename(filename);
        Map<String,Object> state=new HashMap<>();

        String staticResDir=basePath+"staticresource";
        String staticImgResDir=staticResDir+File.separator+"video";

        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");

        Date date=new Date(System.currentTimeMillis());
        String d=dateFormat.format(date);
        System.out.println("日期："+d);

        staticImgResDir+=File.separator+d;
        System.out.println(staticImgResDir);

        File file=new File(staticImgResDir);
        if(!file.exists()){
            file.mkdirs();
        }

        upfile.transferTo(new File(staticImgResDir,filename));
        state.put("state","SUCCESS");

        String url="staticresource"+"/"+"video"+"/"+d+"/"+filename;
        System.out.println("url:"+url);

        state.put("url",url);

        state.put("type",suffix);
        state.put("title",filename);
        state.put("original",filename);
        return state;
    }

    @RequestMapping(value = "/uploadfile")
    public Map<String,Object> uploadFile(@RequestParam MultipartFile upfile,HttpServletRequest request) throws IOException {
        String basePath = ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/");
        System.out.println("当前应用上下文目录："+basePath);

        String filename=upfile.getOriginalFilename();
        System.out.println("原始文件名："+filename);
        String suffix = FileType.getSuffixByFilename(filename);
        Map<String,Object> state=new HashMap<>();

        String staticResDir=basePath+"staticresource";
        String staticImgResDir=staticResDir+File.separator+"file";

        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");

        Date date=new Date(System.currentTimeMillis());
        String d=dateFormat.format(date);
        System.out.println("日期："+d);

        staticImgResDir+=File.separator+d;
        System.out.println(staticImgResDir);

        File file=new File(staticImgResDir);
        if(!file.exists()){
            file.mkdirs();
        }

        upfile.transferTo(new File(staticImgResDir,filename));
        state.put("state","SUCCESS");

        String url="staticresource"+"/"+"file"+"/"+d+"/"+filename;
        System.out.println("url:"+url);

        state.put("url",url);

        state.put("type",suffix);
        state.put("title",filename);
        state.put("original",filename);
        return state;
    }

    @RequestMapping(value = "/pubeva")
    public Map<String,Object> publicEvaluate(@RequestParam String content,@RequestParam String stateid){
        Map<String,Object> json=new HashMap<>();
        if(session.getAttribute("username")==null){
            json.put("result",false);
            json.put("errorinfo","用户未登录");
            return json;
        }
        long userid = (Long) session.getAttribute("userid");
        commentRepository.addComment(userid, content, Long.parseLong(stateid));
        System.out.println("评论的内容："+content);
        json.put("result",true);
        return json;
    }
    @RequestMapping(value = "/forwardstate")
    public Map<String,Object> forwardState(@RequestParam String stateid,@RequestParam String content,@RequestParam boolean comment){
        Map<String,Object> json=new HashMap<>();
        if(session.getAttribute("username")==null){
            json.put("result",false);
            json.put("errorinfo","用户未登录");
            return json;
        }
        System.out.println(comment);
        long userid = (Long) session.getAttribute("userid");

        User user = mUserRepo.findUserByID(userid);

        long dynamicID = mDynamicRepo.forwardState(userid, content, Long.parseLong(stateid));
        Dynamic forwardDynamic = mDynamicRepo.findDynamicByID(Long.parseLong(stateid));

        Dynamic dynamic = mDynamicRepo.findDynamicByID(dynamicID);
        if (comment) {
            publicEvaluate(content, stateid);
        }
        //System.out.println("评论的内容："+content);
        System.out.println("转发成功");
        json.put("dynamicID", dynamicID);
        json.put("userimg", user.getUserimg());
        json.put("username", user.getUsername());
        json.put("time", dynamic.getDynamic_date());
        json.put("dynamic_text", dynamic.getDynamic_word());
        json.put("forward_dynamic", forwardDynamic);
        json.put("result",true);
        return json;
    }

    @RequestMapping(value = "/dianzanstate")
    public Map<String,Object> delDianzan(@RequestParam String stateid){
        Map<String,Object> json=new HashMap<>();
        if(session.getAttribute("username")==null){
            json.put("result",false);
            json.put("errorinfo","用户未登录");
            return json;
        }

        System.out.println("给动态点赞成功");
        long userid = (Long) session.getAttribute("userid");
        if (thumbs_upRepository.findThumbsByID(userid, Long.parseLong(stateid)) == 0) {  //点赞
            thumbs_upRepository.insertID(userid, Long.parseLong(stateid));
        } else {
            thumbs_upRepository.delThumb(userid, Long.parseLong(stateid));
        }
        json.put("result",true);
        return json;
    }
}
